package service

import (
	"context"
	__ "member-srv/basic/proto"
	"member-srv/handler/model"
	"time"
)

type Server struct {
	__.UnimplementedMemberServer
}

func (s *Server) CreateUser(_ context.Context, in *__.CreateUserReq) (*__.CreateUserResp, error) {

	var user model.User
	user = model.User{
		Mobile:     in.Mobile,
		Password:   in.Password,
		Name:       in.Name,
		MemberTime: time.Now(),
	}
	err := user.CreateUser()
	if err != nil {
		return nil, err
	}

	return &__.CreateUserResp{UserId: int64(user.Id)}, nil
}

func (s *Server) AddVideo(_ context.Context, in *__.AddVideoReq) (*__.AddVideoResp, error) {

	var video model.Video

	video = model.Video{
		Title:    in.Title,
		Tag:      in.Tag,
		ImageUrl: in.ImageUrl,
		Info:     in.Info,
		Status:   int8(in.Status),
		VideoUrl: in.VideoUrl,
	}
	err := video.AddVideo()
	if err != nil {
		return nil, err
	}

	return &__.AddVideoResp{VideoId: int64(video.Id)}, nil
}

func (s *Server) GetPageVideo(_ context.Context, in *__.GetPageVideoReq) (*__.GetPageVideoResp, error) {

	var video model.Video
	var listVideo []*__.GetPageVideo
	pageVideo, err := video.GetPageVideo(int(in.Page), int(in.PageSize))
	if err != nil {
		return nil, err
	}

	for _, v := range pageVideo {
		list := &__.GetPageVideo{
			VideoId:  int64(v.Id),
			Title:    v.Title,
			Tag:      v.Tag,
			ImageUrl: v.ImageUrl,
			Info:     v.Info,
			Status:   int64(v.Status),
			VideoUrl: v.VideoUrl,
		}
		listVideo = append(listVideo, list)
	}

	return &__.GetPageVideoResp{List: listVideo}, nil
}
